{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4605fd16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([200, 230, 245, 247, 259, 262])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "data = np.array([\n",
    "    [80, 200],\n",
    "    [95, 230],\n",
    "    [104, 245],\n",
    "    [112, 247],\n",
    "    [125, 259],\n",
    "    [135, 262],\n",
    "])\n",
    "data[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "41577535",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b:15.370000000000001 slop:-14.370000000000001 mse:346779\n",
      "b:28.877800000000004 slop:-13.507800000000003 mse:306718.6014\n",
      "b:41.575132 slop:-12.697332 mse:271321.23319704\n",
      "b:53.51062408000001 slop:-11.935492080000001 mse:240044.11865290458\n",
      "b:64.7299866352 slop:-11.219362555199998 mse:212407.66024170644\n",
      "b:75.276187437088 slop:-10.546200801887998 mse:187988.08558957177\n",
      "b:85.18961619086272 slop:-9.91342875377472 mse:166410.94942694565\n",
      "b:94.50823921941097 slop:-9.318623028548238 mse:147345.3919136492\n",
      "b:103.26774486624632 slop:-8.759505646835343 mse:130499.06529490041\n",
      "b:111.50168017427154 slop:-8.233935308025222 mse:115613.65109457399\n",
      "b:119.24157936381525 slop:-7.739899189543707 mse:102460.89910716558\n",
      "b:126.51708460198633 slop:-7.2755052381710845 mse:90839.1274510915\n",
      "b:133.35605952586715 slop:-6.83897492388082 mse:80570.13001578447\n",
      "b:139.7846959543151 slop:-6.4286364284479705 mse:71496.44388194714\n",
      "b:145.8276141970562 slop:-6.042918242741093 mse:63478.934814088505\n",
      "b:151.50795734523282 slop:-5.680343148176629 mse:56394.66380172862\n",
      "b:156.84747990451885 slop:-5.3395225592860305 mse:50135.0019352074\n",
      "b:161.8666311102477 slop:-5.0191512057288685 mse:44603.964709949265\n",
      "b:166.58463324363285 slop:-4.718002133385138 mse:39716.74021771118\n",
      "b:171.01955524901487 slop:-4.434922005382028 mse:35398.3886563696\n",
      "b:175.188381934074 slop:-4.168826685059108 mse:31582.69321676818\n",
      "b:179.10707901802954 slop:-3.9186970839555615 mse:28211.14472633636\n",
      "b:182.79065427694778 slop:-3.683575258918228 mse:25232.044480190816\n",
      "b:186.2532150203309 slop:-3.462560743383134 mse:22599.711502696595\n",
      "b:189.50802211911105 slop:-3.2548070987801454 mse:20273.78208378271\n",
      "b:192.56754079196438 slop:-3.0595186728533372 mse:18218.590849230408\n",
      "b:195.4434883444465 slop:-2.8759475524821374 mse:16402.623874379995\n",
      "b:198.1468790437797 slop:-2.7033906993332097 mse:14798.035455402169\n",
      "b:200.68806630115293 slop:-2.5411872573732177 mse:13380.221128393361\n",
      "b:203.07678232308376 slop:-2.3887160219308243 mse:12127.44038904837\n",
      "b:205.32217538369875 slop:-2.2453930606149743 mse:11020.483327763135\n",
      "b:207.43284486067682 slop:-2.110669476978075 mse:10042.3760684115\n",
      "b:209.41687416903622 slop:-1.9840293083593907 mse:9178.120494048402\n",
      "b:211.28186171889405 slop:-1.8649875498578268 mse:8414.464268541167\n",
      "b:213.03495001576042 slop:-1.753088296866357 mse:7739.697627682974\n",
      "b:214.68285301481478 slop:-1.6479029990543752 mse:7143.473823820674\n",
      "b:216.23188183392588 slop:-1.549028819111113 mse:6616.650470727948\n",
      "b:217.68796892389034 slop:-1.4560870899644471 mse:6151.149355935219\n",
      "b:219.0566907884569 slop:-1.3687218645665797 mse:5739.832570904357\n",
      "b:220.3432893411495 slop:-1.2865985526925858 mse:5376.393059651094\n",
      "b:221.55269198068052 slop:-1.20940263953103 mse:5055.257907507705\n",
      "b:222.6895304618397 slop:-1.1368384811591687 mse:4771.50288707381\n",
      "b:223.7581586341293 slop:-1.0686281722896183 mse:4520.776951018417\n",
      "b:224.76266911608155 slop:-1.004510481952242 mse:4299.235513919875\n",
      "b:225.70690896911665 slop:-0.9442398530351068 mse:4103.4815000996\n",
      "b:226.59449443096966 slop:-0.8875854618530008 mse:3930.5132534880076\n",
      "b:227.42882476511147 slop:-0.8343303341418203 mse:3777.6785107820024\n",
      "b:228.2130952792048 slop:-0.7842705140933117 mse:3642.6337321269793\n",
      "b:228.9503095624525 slop:-0.7372142832477124 mse:3523.308165707397\n",
      "b:229.64329098870536 slop:-0.6929814262528499 mse:3417.872095219056\n",
      "b:230.29469352938304 slop:-0.6514025406776784 mse:3324.708783335557\n",
      "b:230.90701191762005 slop:-0.6123183882370177 mse:3242.3896809552984\n",
      "b:231.48259120256284 slop:-0.5755792849427968 mse:3169.6525220921017\n",
      "b:232.02363573040907 slop:-0.5410445278462294 mse:3105.381968520582\n",
      "b:232.53221758658452 slop:-0.5085818561754559 mse:3048.592507384786\n",
      "b:233.01028453138946 slop:-0.4780669448049287 mse:2998.413339525198\n",
      "b:233.45966745950608 slop:-0.44938292811663244 mse:2954.075026804464\n",
      "b:233.88208741193571 slop:-0.42241995242963526 mse:2914.8976936844256\n",
      "b:234.27916216721957 slop:-0.39707475528385716 mse:2880.2806021395586\n",
      "b:234.6524124371864 slop:-0.373250269966826 mse:2849.692940050514\n",
      "b:235.00326769095523 slop:-0.35085525376881604 mse:2822.665681828634\n",
      "b:235.3330716294979 slop:-0.3298039385426864 mse:2798.78439646378\n",
      "b:235.64308733172803 slop:-0.3100157022301255 mse:2777.682892715396\n",
      "b:235.93450209182436 slop:-0.29141476009631845 mse:2759.0376040033243\n",
      "b:236.2084319663149 slop:-0.2739298744905386 mse:2742.562626897337\n",
      "b:236.465926048336 slop:-0.2574940820211066 mse:2728.0053371264876\n",
      "b:236.70797048543585 slop:-0.24204443709983992 mse:2715.1425158849643\n",
      "b:236.9354922563097 slop:-0.22752177087384895 mse:2703.776927035954\n",
      "b:237.1493627209311 slop:-0.21387046462141882 mse:2693.734292728969\n",
      "b:237.35040095767522 slop:-0.20103823674413435 mse:2684.860621055318\n",
      "b:237.5393769002147 slop:-0.18897594253948682 mse:2677.0198447644784\n",
      "b:237.71701428620182 slop:-0.17763738598711767 mse:2670.091734833894\n",
      "b:237.88399342902972 slop:-0.166979142827891 mse:2663.970056899229\n",
      "b:238.04095382328794 slop:-0.1569603942582171 mse:2658.5609422761586\n",
      "b:238.18849659389065 slop:-0.14754277060272386 mse:2653.7814485952135\n",
      "b:238.3271867982572 slop:-0.13869020436656115 mse:2649.558287978731\n",
      "b:238.45755559036178 slop:-0.1303687921045679 mse:2645.8267032580065\n",
      "b:238.58010225494007 slop:-0.1225466645782933 mse:2642.5294749987743\n",
      "b:238.69529611964367 slop:-0.11519386470359563 mse:2639.6160441089173\n",
      "b:238.80357835246505 slop:-0.10828223282137969 mse:2637.041736574639\n",
      "b:238.90536365131715 slop:-0.10178529885209685 mse:2634.767078437351\n",
      "b:239.00104183223812 slop:-0.09567818092097127 mse:2632.7571905072436\n",
      "b:239.09097932230384 slop:-0.08993749006571307 mse:2630.9812535322008\n",
      "b:239.1755205629656 slop:-0.08454124066176974 mse:2629.412035621052\n",
      "b:239.25498932918765 slop:-0.07946876622206389 mse:2628.025474674762\n",
      "b:239.3296899694364 slop:-0.07470064024874092 mse:2626.80030942262\n",
      "b:239.39990857127023 slop:-0.07021860183381591 mse:2625.7177534058264\n",
      "b:239.46591405699402 slop:-0.06600548572378614 mse:2624.7612069093884\n",
      "b:239.52795921357438 slop:-0.06204515658035859 mse:2623.9160024251355\n",
      "b:239.5862816607599 slop:-0.058322447185537384 mse:2623.1691797428502\n",
      "b:239.64110476111432 slop:-0.054823100354405484 mse:2622.509287220782\n",
      "b:239.69263847544747 slop:-0.05153371433314078 mse:2621.9262061882832\n",
      "b:239.74108016692063 slop:-0.04844169147315199 mse:2621.410995787967\n",
      "b:239.7866153569054 slop:-0.0455351899847625 mse:2620.955755878247\n",
      "b:239.82941843549108 slop:-0.04280307858567596 mse:2620.5535058940195\n",
      "b:239.86965332936163 slop:-0.040234893870535304 mse:2620.1980778079555\n",
      "b:239.90747412959993 slop:-0.03782080023830247 mse:2619.884021551109\n",
      "b:239.94302568182394 slop:-0.03555155222400402 mse:2619.60652144256\n",
      "b:239.9764441409145 slop:-0.0334184590905636 mse:2619.361322346646\n",
      "b:240.00785749245964 slop:-0.03141335154512945 mse:2619.1446644254966\n",
      "b:240.03738604291206 slop:-0.029528550452421882 mse:2618.953225486369\n",
      "b:240.06514288033733 slop:-0.02775683742527633 mse:2618.7840700397555\n",
      "b:240.09123430751708 slop:-0.02609142717975999 mse:2618.634604287128\n",
      "b:240.11576024906606 slop:-0.024525941548974972 mse:2618.502536348106\n",
      "b:240.1388146341221 slop:-0.02305438505603661 mse:2618.3858411171864\n",
      "b:240.16048575607476 slop:-0.02167112195267407 mse:2618.282729211146\n",
      "b:240.18085661071026 slop:-0.020370854635514208 mse:2618.1916195309686\n",
      "b:240.20000521406766 slop:-0.019148603357384105 mse:2618.111115017564\n",
      "b:240.2180049012236 slop:-0.01799968715594048 mse:2618.0399812295195\n",
      "b:240.2349246071502 slop:-0.016919705926584358 mse:2617.977127414403\n",
      "b:240.25082913072117 slop:-0.015904523570988545 mse:2617.921589783367\n",
      "b:240.2657793828779 slop:-0.014950252156729675 mse:2617.872516732583\n",
      "b:240.27983261990522 slop:-0.014053237027326304 mse:2617.82915578491\n",
      "b:240.29304266271092 slop:-0.013210042805686726 mse:2617.790842051547\n",
      "b:240.30546010294827 slop:-0.012417440237344977 mse:2617.756988036747\n",
      "b:240.31713249677136 slop:-0.011672393823103562 mse:2617.7270746292693\n",
      "b:240.32810454696508 slop:-0.010972050193718132 mse:2617.700643142422\n",
      "b:240.3384182741472 slop:-0.010313727182095249 mse:2617.6772882806445\n",
      "b:240.34811317769837 slop:-0.00969490355116875 mse:2617.656651924777\n",
      "b:240.35722638703646 slop:-0.009113209338098045 mse:2617.638417640733\n",
      "b:240.3657928038143 slop:-0.008566416777812265 mse:2617.6223058273526\n",
      "b:240.37384523558543 slop:-0.008052431771142779 mse:2617.608069429048\n",
      "b:240.3814145214503 slop:-0.0075692858648739045 mse:2617.5954901475066\n",
      "b:240.38852965016326 slop:-0.007115128712982255 mse:2617.5843750943372\n",
      "b:240.39521787115348 slop:-0.006688220990204172 mse:2617.5745538333563\n",
      "b:240.40150479888428 slop:-0.0062869277307913765 mse:2617.5658757671536\n",
      "b:240.4074145109512 slop:-0.005909712066943485 mse:2617.5582078278567\n",
      "b:240.41296964029414 slop:-0.005555129342927217 mse:2617.5514324366945\n",
      "b:240.4181914618765 slop:-0.005221821582351822 mse:2617.545445701063\n",
      "b:240.4230999741639 slop:-0.004908512287410645 mse:2617.5401558214594\n",
      "b:240.42771397571406 slop:-0.0046140015501657675 mse:2617.5354816838417\n",
      "b:240.43205113717121 slop:-0.00433716145715664 mse:2617.5313516158426\n",
      "b:240.43612806894095 slop:-0.0040769317697271394 mse:2617.527702287758\n",
      "b:240.4399603848045 slop:-0.003832315863543272 mse:2617.5244777414637\n",
      "b:240.44356276171624 slop:-0.003602376911730403 mse:2617.5216285323568\n",
      "b:240.44694899601328 slop:-0.0033862342970257943 mse:2617.5191109711905\n",
      "b:240.45013205625247 slop:-0.003183060239203428 mse:2617.516886454144\n",
      "b:240.45312413287732 slop:-0.002992076624851734 mse:2617.5149208708813\n",
      "b:240.45593668490469 slop:-0.0028125520273607663 mse:2617.513184081511\n",
      "b:240.4585804838104 slop:-0.0026437989057188816 mse:2617.511649454423\n",
      "b:240.46106565478178 slop:-0.00248517097137551 mse:2617.510293457928\n",
      "b:240.46340171549488 slop:-0.0023360607130933886 mse:2617.509095299425\n",
      "b:240.46559761256518 slop:-0.0021958970703070692 mse:2617.508036606572\n",
      "b:240.46766175581126 slop:-0.0020641432460894293 mse:2617.5071011455675\n",
      "b:240.46960205046258 slop:-0.001940294651324166 mse:2617.5062745722234\n",
      "b:240.47142592743484 slop:-0.0018238769722449888 mse:2617.5055442120165\n",
      "b:240.47314037178876 slop:-0.001714444353909812 mse:2617.5048988657377\n",
      "b:240.47475194948143 slop:-0.001611577692674473 mse:2617.504328637766\n",
      "b:240.47626683251255 slop:-0.0015148830311142092 mse:2617.50382478433\n",
      "b:240.47769082256178 slop:-0.0014239900492469815 mse:2617.5033795794343\n",
      "b:240.47902937320808 slop:-0.0013385506462930152 mse:2617.502986196388\n",
      "b:240.4802876108156 slop:-0.001258237607515298 mse:2617.502638603128\n",
      "b:240.48147035416665 slop:-0.001182743351064346 mse:2617.5023314697246\n",
      "b:240.48258213291666 slop:-0.0011117787500012355 mse:2617.5020600866483\n",
      "b:240.48362720494166 slop:-0.0010450720250003087 mse:2617.501820292562\n",
      "b:240.48460957264516 slop:-0.0009823677035006996 mse:2617.501608410508\n",
      "b:240.48553299828646 slop:-0.0009234256412906916 mse:2617.501421191525\n",
      "b:240.48640101838927 slop:-0.0008680201028124657 mse:2617.5012557648315\n",
      "b:240.4872169572859 slop:-0.0008159388966436155 mse:2617.501109593805\n",
      "b:240.48798393984876 slop:-0.0007669825628454418 mse:2617.5009804370857\n",
      "b:240.48870490345783 slop:-0.0007209636090743743 mse:2617.5008663142094\n",
      "b:240.48938260925036 slop:-0.0006777057925302188 mse:2617.5007654752353\n",
      "b:240.49001965269534 slop:-0.0006370434449786444 mse:2617.5006763739184\n",
      "b:240.49061847353363 slop:-0.0005988208382797212 mse:2617.500597643994\n",
      "b:240.4911813651216 slop:-0.0005628915879822217 mse:2617.5005280782334\n",
      "b:240.49171048321432 slop:-0.0005291180927036976 mse:2617.500466609927\n",
      "b:240.49220785422145 slop:-0.0004973710071408277 mse:2617.500412296531\n",
      "b:240.49267538296817 slop:-0.0004675287467131284 mse:2617.500364305215\n",
      "b:240.49311485999007 slop:-0.00043947702190962447 mse:2617.5003219000882\n",
      "b:240.49352796839068 slop:-0.0004131084005956609 mse:2617.500284430918\n",
      "b:240.49391629028725 slop:-0.00038832189655920503 mse:2617.500251323159\n",
      "b:240.49428131287002 slop:-0.00036502258276527755 mse:2617.5002220691435\n",
      "b:240.4946244340978 slop:-0.0003431212277990881 mse:2617.5001962202946\n",
      "b:240.49494696805192 slop:-0.00032253395413192723 mse:2617.500173380253\n",
      "b:240.4952501499688 slop:-0.00030318191688479603 mse:2617.500153198791\n",
      "b:240.49553514097067 slop:-0.00028499100187218574 mse:2617.500135366452\n",
      "b:240.49580303251244 slop:-0.0002678915417595817 mse:2617.500119609797\n",
      "b:240.4960548505617 slop:-0.0002518180492535294 mse:2617.5001056872165\n",
      "b:240.496291559528 slop:-0.0002367089662976696 mse:2617.5000933852243\n",
      "b:240.49651406595632 slop:-0.0002225064283197753 mse:2617.500082515184\n",
      "b:240.49672322199893 slop:-0.00020915604262086162 mse:2617.5000729104167\n",
      "b:240.496919828679 slop:-0.000196606680063951 mse:2617.5000644236443\n",
      "b:240.49710463895826 slop:-0.0001848102792592954 mse:2617.5000569247322\n",
      "b:240.49727836062075 slop:-0.00017372166250424927 mse:2617.500050298693\n",
      "b:240.49744165898352 slop:-0.00016329836275474463 mse:2617.5000444439256\n",
      "b:240.4975951594445 slop:-0.00015350046098888014 mse:2617.5000392706525\n",
      "b:240.49773944987783 slop:-0.00014429043332995662 mse:2617.5000346995485\n",
      "b:240.49787508288514 slop:-0.00013563300733039797 mse:2617.5000306605216\n",
      "b:240.49800257791205 slop:-0.0001274950268913244 mse:2617.500027091636\n",
      "b:240.49812242323733 slop:-0.00011984532527719694 mse:2617.5000239381698\n",
      "b:240.4982350778431 slop:-0.00011265460575998531 mse:2617.500021151767\n",
      "b:240.49834097317253 slop:-0.00010589532941366997 mse:2617.500018689701\n",
      "b:240.49844051478217 slop:-9.954160964809945e-05 mse:2617.50001651422\n",
      "b:240.49853408389524 slop:-9.356911306952043e-05 mse:2617.500014591965\n",
      "b:240.49862203886153 slop:-8.795496628579258e-05 mse:2617.5000128934603\n",
      "b:240.49870471652983 slop:-8.267766830840629e-05 mse:2617.5000113926617\n",
      "b:240.49878243353805 slop:-7.771700821024297e-05 mse:2617.5000100665557\n",
      "b:240.49885548752576 slop:-7.305398771677574e-05 mse:2617.500008894809\n",
      "b:240.4989241582742 slop:-6.867074845445132e-05 mse:2617.500007859453\n",
      "b:240.49898870877774 slop:-6.455050354759351e-05 mse:2617.500006944613\n",
      "b:240.49904938625107 slop:-6.06774733353177e-05 mse:2617.50000613626\n",
      "b:240.499106423076 slop:-5.703682493560791e-05 mse:2617.500005421999\n",
      "b:240.49916003769144 slop:-5.3614615439983026e-05 mse:2617.5000047908784\n",
      "b:240.49921043542994 slop:-5.0397738513652256e-05 mse:2617.5000042332204\n",
      "b:240.49925780930414 slop:-4.737387420334471e-05 mse:2617.5000037404734\n",
      "b:240.49930234074588 slop:-4.453144175158741e-05 mse:2617.500003305082\n",
      "b:240.49934420030112 slop:-4.1859555246901436e-05 mse:2617.5000029203707\n",
      "b:240.49938354828305 slop:-3.9347981932564835e-05 mse:2617.5000025804397\n",
      "b:240.49942053538607 slop:-3.698710301705432e-05 mse:2617.5000022800764\n",
      "b:240.4994553032629 slop:-3.4767876836099274e-05 mse:2617.5000020146754\n",
      "b:240.4994879850671 slop:-3.2681804226513124e-05 mse:2617.5000017801676\n",
      "b:240.49951870596308 slop:-3.0720895973672666e-05 mse:2617.5000015729556\n",
      "b:240.4995475836053 slop:-2.887764221497946e-05 mse:2617.500001389864\n",
      "b:240.49957472858898 slop:-2.7144983681637315e-05 mse:2617.5000012280834\n",
      "b:240.49960024487365 slop:-2.5516284661080136e-05 mse:2617.500001085135\n",
      "b:240.49962423018124 slop:-2.398530758114248e-05 mse:2617.500000958825\n",
      "b:240.49964677637035 slop:-2.254618912559181e-05 mse:2617.500000847218\n",
      "b:240.49966796978813 slop:-2.1193417778704317e-05 mse:2617.5000007486015\n",
      "b:240.49968789160084 slop:-1.9921812711913844e-05 mse:2617.500000661464\n",
      "b:240.4997066181048 slop:-1.8726503949437756e-05 mse:2617.5000005844695\n",
      "b:240.4997242210185 slop:-1.7602913712266854e-05 mse:2617.5000005164375\n",
      "b:240.4997407677574 slop:-1.6546738889360315e-05 mse:2617.500000456324\n",
      "b:240.49975632169196 slop:-1.5553934555896376e-05 mse:2617.500000403208\n",
      "b:240.49977094239046 slop:-1.4620698482303852e-05 mse:2617.500000356275\n",
      "b:240.49978468584703 slop:-1.37434565726835e-05 mse:2617.5000003148048\n",
      "b:240.4997976046962 slop:-1.291884917804964e-05 mse:2617.5000002781608\n",
      "b:240.49980974841444 slop:-1.2143718228116996e-05 mse:2617.500000245783\n",
      "b:240.49982116350958 slop:-1.1415095133884279e-05 mse:2617.500000217174\n",
      "b:240.499831893699 slop:-1.0730189425203207e-05 mse:2617.5000001918947\n",
      "b:240.49984198007706 slop:-1.0086378060236711e-05 mse:2617.5000001695585\n",
      "b:240.49985146127244 slop:-9.481195376110917e-06 mse:2617.5000001498215\n",
      "b:240.4998603735961 slop:-8.912323653476051e-06 mse:2617.5000001323824\n",
      "b:240.49986875118034 slop:-8.377584233585367e-06 mse:2617.5000001169733\n",
      "b:240.4998766261095 slop:-7.874929179365609e-06 mse:2617.5000001033577\n",
      "b:240.49988402854294 slop:-7.4024334293198994e-06 mse:2617.500000091327\n",
      "b:240.49989098683037 slop:-6.958287423799447e-06 mse:2617.5000000806963\n",
      "b:240.49989752762053 slop:-6.540790178064526e-06 mse:2617.500000071303\n",
      "b:240.4999036759633 slop:-6.14834276802867e-06 mse:2617.5000000630034\n",
      "b:240.49990945540551 slop:-5.779442201401252e-06 mse:2617.50000005567\n",
      "b:240.49991488808118 slop:-5.432675669112541e-06 mse:2617.50000004919\n",
      "b:240.4999199947963 slop:-5.106715129272743e-06 mse:2617.5000000434643\n",
      "b:240.4999247951085 slop:-4.8003122219597576e-06 mse:2617.500000038405\n",
      "b:240.499929307402 slop:-4.5122934892560805e-06 mse:2617.500000033935\n",
      "b:240.49993354895787 slop:-4.241555880071246e-06 mse:2617.500000029984\n",
      "b:240.4999375360204 slop:-3.9870625278126684e-06 mse:2617.5000000264945\n",
      "b:240.4999412838592 slop:-3.747838775325363e-06 mse:2617.500000023411\n",
      "b:240.49994480682764 slop:-3.522968448805841e-06 mse:2617.5000000206855\n",
      "b:240.499948118418 slop:-3.311590341468218e-06 mse:2617.5000000182777\n",
      "b:240.4999512313129 slop:-3.1128949206049582e-06 mse:2617.50000001615\n",
      "b:240.49995415743413 slop:-2.9261212256415093e-06 mse:2617.5000000142704\n",
      "b:240.4999569079881 slop:-2.7505539520689127e-06 mse:2617.5000000126092\n",
      "b:240.4999594935088 slop:-2.585520714433187e-06 mse:2617.5000000111418\n",
      "b:240.49996192389827 slop:-2.4303894713284536e-06 mse:2617.5000000098444\n",
      "b:240.49996420846438 slop:-2.2845661038672915e-06 mse:2617.500000008699\n",
      "b:240.4999663559565 slop:-2.1474921373965117e-06 mse:2617.500000007686\n",
      "b:240.49996837459912 slop:-2.018642609868948e-06 mse:2617.5000000067917\n",
      "b:240.49997027212316 slop:-1.8975240527652204e-06 mse:2617.500000006001\n",
      "b:240.49997205579578 slop:-1.7836726101450041e-06 mse:2617.500000005303\n",
      "b:240.49997373244804 slop:-1.6766522531952433e-06 mse:2617.500000004685\n",
      "b:240.49997530850115 slop:-1.57605311756015e-06 mse:2617.5000000041396\n",
      "b:240.49997678999108 slop:-1.481489931052238e-06 mse:2617.500000003658\n",
      "b:240.4999781825916 slop:-1.3926005351549974e-06 mse:2617.5000000032323\n",
      "b:240.4999794916361 slop:-1.3090445037278186e-06 mse:2617.500000002856\n",
      "b:240.49998072213793 slop:-1.2305018333336193e-06 mse:2617.5000000025234\n",
      "b:240.49998187880965 slop:-1.156671723947511e-06 mse:2617.500000002229\n",
      "b:240.49998296608106 slop:-1.0872714210563573e-06 mse:2617.50000000197\n",
      "b:240.4999839881162 slop:-1.0220351362022485e-06 mse:2617.500000001741\n",
      "b:240.49998494882925 slop:-9.607130272115682e-07 mse:2617.5000000015384\n",
      "b:240.4999858518995 slop:-9.030702449308592e-07 mse:2617.5000000013592\n",
      "b:240.49998670078554 slop:-8.488860294164625e-07 mse:2617.5000000012005\n",
      "b:240.49998749873842 slop:-7.979528675150505e-07 mse:2617.5000000010614\n",
      "b:240.4999882488141 slop:-7.500756947820264e-07 mse:2617.5000000009377\n",
      "b:240.49998895388526 slop:-7.050711536749077e-07 mse:2617.500000000828\n",
      "b:240.49998961665216 slop:-6.627668841474588e-07 mse:2617.5000000007317\n",
      "b:240.49999023965302 slop:-6.230008705188084e-07 mse:2617.5000000006467\n",
      "b:240.49999082527384 slop:-5.85620819038013e-07 mse:2617.500000000572\n",
      "b:240.49999137575742 slop:-5.504835695546718e-07 mse:2617.500000000505\n",
      "b:240.49999189321198 slop:-5.174545549380128e-07 mse:2617.5000000004466\n",
      "b:240.49999237961927 slop:-4.86407280959611e-07 mse:2617.5000000003943\n",
      "b:240.49999283684213 slop:-4.572228436927617e-07 mse:2617.5000000003483\n",
      "b:240.4999932666316 slop:-4.297894724913931e-07 mse:2617.500000000308\n",
      "b:240.4999936706337 slop:-4.040021036644248e-07 mse:2617.500000000272\n",
      "b:240.49999405039569 slop:-3.797619780243622e-07 mse:2617.5000000002406\n",
      "b:240.49999440737196 slop:-3.5697625889952177e-07 mse:2617.5000000002124\n",
      "b:240.49999474292963 slop:-3.355576825470052e-07 mse:2617.5000000001874\n",
      "b:240.49999505835385 slop:-3.154242222080939e-07 mse:2617.5000000001664\n",
      "b:240.49999535485262 slop:-2.964987692166687e-07 mse:2617.500000000147\n",
      "b:240.49999563356147 slop:-2.787088430977747e-07 mse:2617.5000000001296\n",
      "b:240.49999589554778 slop:-2.619863118979993e-07 mse:2617.5000000001146\n",
      "b:240.4999961418149 slop:-2.46267132979483e-07 mse:2617.500000000101\n",
      "b:240.499996373306 slop:-2.3149110575104715e-07 mse:2617.5000000000896\n",
      "b:240.49999659090764 slop:-2.1760163974704484e-07 mse:2617.500000000079\n",
      "b:240.49999679545317 slop:-2.045455414645403e-07 mse:2617.5000000000696\n",
      "b:240.49999698772598 slop:-1.92272809727001e-07 mse:2617.500000000062\n",
      "b:240.49999716846241 slop:-1.8073644127980515e-07 mse:2617.5000000000546\n",
      "b:240.49999733835466 slop:-1.6989225514407736e-07 mse:2617.500000000048\n",
      "b:240.49999749805338 slop:-1.5969872038112954e-07 mse:2617.5000000000427\n",
      "b:240.49999764817017 slop:-1.5011679749932227e-07 mse:2617.5000000000377\n",
      "b:240.49999778927994 slop:-1.4110979009274161e-07 mse:2617.500000000033\n",
      "b:240.49999792192315 slop:-1.3264320330108604e-07 mse:2617.5000000000296\n"
     ]
    }
   ],
   "source": [
    "# 随机定义一个b值\n",
    "b = 1\n",
    "\n",
    "learning_rate = 0.01\n",
    "\n",
    "# b的所有历史记录\n",
    "b_history = []\n",
    "# mse的所有历史记录\n",
    "mse_history = []\n",
    "\n",
    "# y=0*x + b\n",
    "\n",
    "def gradent_decent():\n",
    "    global b\n",
    "    \n",
    "    # 梯度下降\n",
    "#     slop = np.sum([b - item for item in data[:, -1]])\n",
    "    slop = 0\n",
    "    mse = 0\n",
    "    for item in data[:, 1]:\n",
    "        slop += (b - item)\n",
    "        mse  += (b - item) ** 2\n",
    "        \n",
    "    b = b - slop * learning_rate\n",
    "    \n",
    "    b_history.append(b)\n",
    "    mse_history.append(mse)\n",
    "    \n",
    "    print(\"b:{} slop:{} mse:{}\".format(b, slop * learning_rate,mse))\n",
    "    \n",
    "for _ in range(300):\n",
    "    gradent_decent()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c8ebfcc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "/* Put everything inside the global mpl namespace */\n",
       "/* global mpl */\n",
       "window.mpl = {};\n",
       "\n",
       "mpl.get_websocket_type = function () {\n",
       "    if (typeof WebSocket !== 'undefined') {\n",
       "        return WebSocket;\n",
       "    } else if (typeof MozWebSocket !== 'undefined') {\n",
       "        return MozWebSocket;\n",
       "    } else {\n",
       "        alert(\n",
       "            'Your browser does not have WebSocket support. ' +\n",
       "                'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
       "                'Firefox 4 and 5 are also supported but you ' +\n",
       "                'have to enable WebSockets in about:config.'\n",
       "        );\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure = function (figure_id, websocket, ondownload, parent_element) {\n",
       "    this.id = figure_id;\n",
       "\n",
       "    this.ws = websocket;\n",
       "\n",
       "    this.supports_binary = this.ws.binaryType !== undefined;\n",
       "\n",
       "    if (!this.supports_binary) {\n",
       "        var warnings = document.getElementById('mpl-warnings');\n",
       "        if (warnings) {\n",
       "            warnings.style.display = 'block';\n",
       "            warnings.textContent =\n",
       "                'This browser does not support binary websocket messages. ' +\n",
       "                'Performance may be slow.';\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.imageObj = new Image();\n",
       "\n",
       "    this.context = undefined;\n",
       "    this.message = undefined;\n",
       "    this.canvas = undefined;\n",
       "    this.rubberband_canvas = undefined;\n",
       "    this.rubberband_context = undefined;\n",
       "    this.format_dropdown = undefined;\n",
       "\n",
       "    this.image_mode = 'full';\n",
       "\n",
       "    this.root = document.createElement('div');\n",
       "    this.root.setAttribute('style', 'display: inline-block');\n",
       "    this._root_extra_style(this.root);\n",
       "\n",
       "    parent_element.appendChild(this.root);\n",
       "\n",
       "    this._init_header(this);\n",
       "    this._init_canvas(this);\n",
       "    this._init_toolbar(this);\n",
       "\n",
       "    var fig = this;\n",
       "\n",
       "    this.waiting = false;\n",
       "\n",
       "    this.ws.onopen = function () {\n",
       "        fig.send_message('supports_binary', { value: fig.supports_binary });\n",
       "        fig.send_message('send_image_mode', {});\n",
       "        if (fig.ratio !== 1) {\n",
       "            fig.send_message('set_dpi_ratio', { dpi_ratio: fig.ratio });\n",
       "        }\n",
       "        fig.send_message('refresh', {});\n",
       "    };\n",
       "\n",
       "    this.imageObj.onload = function () {\n",
       "        if (fig.image_mode === 'full') {\n",
       "            // Full images could contain transparency (where diff images\n",
       "            // almost always do), so we need to clear the canvas so that\n",
       "            // there is no ghosting.\n",
       "            fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
       "        }\n",
       "        fig.context.drawImage(fig.imageObj, 0, 0);\n",
       "    };\n",
       "\n",
       "    this.imageObj.onunload = function () {\n",
       "        fig.ws.close();\n",
       "    };\n",
       "\n",
       "    this.ws.onmessage = this._make_on_message_function(this);\n",
       "\n",
       "    this.ondownload = ondownload;\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._init_header = function () {\n",
       "    var titlebar = document.createElement('div');\n",
       "    titlebar.classList =\n",
       "        'ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix';\n",
       "    var titletext = document.createElement('div');\n",
       "    titletext.classList = 'ui-dialog-title';\n",
       "    titletext.setAttribute(\n",
       "        'style',\n",
       "        'width: 100%; text-align: center; padding: 3px;'\n",
       "    );\n",
       "    titlebar.appendChild(titletext);\n",
       "    this.root.appendChild(titlebar);\n",
       "    this.header = titletext;\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function (_canvas_div) {};\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function (_canvas_div) {};\n",
       "\n",
       "mpl.figure.prototype._init_canvas = function () {\n",
       "    var fig = this;\n",
       "\n",
       "    var canvas_div = (this.canvas_div = document.createElement('div'));\n",
       "    canvas_div.setAttribute(\n",
       "        'style',\n",
       "        'border: 1px solid #ddd;' +\n",
       "            'box-sizing: content-box;' +\n",
       "            'clear: both;' +\n",
       "            'min-height: 1px;' +\n",
       "            'min-width: 1px;' +\n",
       "            'outline: 0;' +\n",
       "            'overflow: hidden;' +\n",
       "            'position: relative;' +\n",
       "            'resize: both;'\n",
       "    );\n",
       "\n",
       "    function on_keyboard_event_closure(name) {\n",
       "        return function (event) {\n",
       "            return fig.key_event(event, name);\n",
       "        };\n",
       "    }\n",
       "\n",
       "    canvas_div.addEventListener(\n",
       "        'keydown',\n",
       "        on_keyboard_event_closure('key_press')\n",
       "    );\n",
       "    canvas_div.addEventListener(\n",
       "        'keyup',\n",
       "        on_keyboard_event_closure('key_release')\n",
       "    );\n",
       "\n",
       "    this._canvas_extra_style(canvas_div);\n",
       "    this.root.appendChild(canvas_div);\n",
       "\n",
       "    var canvas = (this.canvas = document.createElement('canvas'));\n",
       "    canvas.classList.add('mpl-canvas');\n",
       "    canvas.setAttribute('style', 'box-sizing: content-box;');\n",
       "\n",
       "    this.context = canvas.getContext('2d');\n",
       "\n",
       "    var backingStore =\n",
       "        this.context.backingStorePixelRatio ||\n",
       "        this.context.webkitBackingStorePixelRatio ||\n",
       "        this.context.mozBackingStorePixelRatio ||\n",
       "        this.context.msBackingStorePixelRatio ||\n",
       "        this.context.oBackingStorePixelRatio ||\n",
       "        this.context.backingStorePixelRatio ||\n",
       "        1;\n",
       "\n",
       "    this.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
       "\n",
       "    var rubberband_canvas = (this.rubberband_canvas = document.createElement(\n",
       "        'canvas'\n",
       "    ));\n",
       "    rubberband_canvas.setAttribute(\n",
       "        'style',\n",
       "        'box-sizing: content-box; position: absolute; left: 0; top: 0; z-index: 1;'\n",
       "    );\n",
       "\n",
       "    // Apply a ponyfill if ResizeObserver is not implemented by browser.\n",
       "    if (this.ResizeObserver === undefined) {\n",
       "        if (window.ResizeObserver !== undefined) {\n",
       "            this.ResizeObserver = window.ResizeObserver;\n",
       "        } else {\n",
       "            var obs = _JSXTOOLS_RESIZE_OBSERVER({});\n",
       "            this.ResizeObserver = obs.ResizeObserver;\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.resizeObserverInstance = new this.ResizeObserver(function (entries) {\n",
       "        var nentries = entries.length;\n",
       "        for (var i = 0; i < nentries; i++) {\n",
       "            var entry = entries[i];\n",
       "            var width, height;\n",
       "            if (entry.contentBoxSize) {\n",
       "                if (entry.contentBoxSize instanceof Array) {\n",
       "                    // Chrome 84 implements new version of spec.\n",
       "                    width = entry.contentBoxSize[0].inlineSize;\n",
       "                    height = entry.contentBoxSize[0].blockSize;\n",
       "                } else {\n",
       "                    // Firefox implements old version of spec.\n",
       "                    width = entry.contentBoxSize.inlineSize;\n",
       "                    height = entry.contentBoxSize.blockSize;\n",
       "                }\n",
       "            } else {\n",
       "                // Chrome <84 implements even older version of spec.\n",
       "                width = entry.contentRect.width;\n",
       "                height = entry.contentRect.height;\n",
       "            }\n",
       "\n",
       "            // Keep the size of the canvas and rubber band canvas in sync with\n",
       "            // the canvas container.\n",
       "            if (entry.devicePixelContentBoxSize) {\n",
       "                // Chrome 84 implements new version of spec.\n",
       "                canvas.setAttribute(\n",
       "                    'width',\n",
       "                    entry.devicePixelContentBoxSize[0].inlineSize\n",
       "                );\n",
       "                canvas.setAttribute(\n",
       "                    'height',\n",
       "                    entry.devicePixelContentBoxSize[0].blockSize\n",
       "                );\n",
       "            } else {\n",
       "                canvas.setAttribute('width', width * fig.ratio);\n",
       "                canvas.setAttribute('height', height * fig.ratio);\n",
       "            }\n",
       "            canvas.setAttribute(\n",
       "                'style',\n",
       "                'width: ' + width + 'px; height: ' + height + 'px;'\n",
       "            );\n",
       "\n",
       "            rubberband_canvas.setAttribute('width', width);\n",
       "            rubberband_canvas.setAttribute('height', height);\n",
       "\n",
       "            // And update the size in Python. We ignore the initial 0/0 size\n",
       "            // that occurs as the element is placed into the DOM, which should\n",
       "            // otherwise not happen due to the minimum size styling.\n",
       "            if (fig.ws.readyState == 1 && width != 0 && height != 0) {\n",
       "                fig.request_resize(width, height);\n",
       "            }\n",
       "        }\n",
       "    });\n",
       "    this.resizeObserverInstance.observe(canvas_div);\n",
       "\n",
       "    function on_mouse_event_closure(name) {\n",
       "        return function (event) {\n",
       "            return fig.mouse_event(event, name);\n",
       "        };\n",
       "    }\n",
       "\n",
       "    rubberband_canvas.addEventListener(\n",
       "        'mousedown',\n",
       "        on_mouse_event_closure('button_press')\n",
       "    );\n",
       "    rubberband_canvas.addEventListener(\n",
       "        'mouseup',\n",
       "        on_mouse_event_closure('button_release')\n",
       "    );\n",
       "    rubberband_canvas.addEventListener(\n",
       "        'dblclick',\n",
       "        on_mouse_event_closure('dblclick')\n",
       "    );\n",
       "    // Throttle sequential mouse events to 1 every 20ms.\n",
       "    rubberband_canvas.addEventListener(\n",
       "        'mousemove',\n",
       "        on_mouse_event_closure('motion_notify')\n",
       "    );\n",
       "\n",
       "    rubberband_canvas.addEventListener(\n",
       "        'mouseenter',\n",
       "        on_mouse_event_closure('figure_enter')\n",
       "    );\n",
       "    rubberband_canvas.addEventListener(\n",
       "        'mouseleave',\n",
       "        on_mouse_event_closure('figure_leave')\n",
       "    );\n",
       "\n",
       "    canvas_div.addEventListener('wheel', function (event) {\n",
       "        if (event.deltaY < 0) {\n",
       "            event.step = 1;\n",
       "        } else {\n",
       "            event.step = -1;\n",
       "        }\n",
       "        on_mouse_event_closure('scroll')(event);\n",
       "    });\n",
       "\n",
       "    canvas_div.appendChild(canvas);\n",
       "    canvas_div.appendChild(rubberband_canvas);\n",
       "\n",
       "    this.rubberband_context = rubberband_canvas.getContext('2d');\n",
       "    this.rubberband_context.strokeStyle = '#000000';\n",
       "\n",
       "    this._resize_canvas = function (width, height, forward) {\n",
       "        if (forward) {\n",
       "            canvas_div.style.width = width + 'px';\n",
       "            canvas_div.style.height = height + 'px';\n",
       "        }\n",
       "    };\n",
       "\n",
       "    // Disable right mouse context menu.\n",
       "    this.rubberband_canvas.addEventListener('contextmenu', function (_e) {\n",
       "        event.preventDefault();\n",
       "        return false;\n",
       "    });\n",
       "\n",
       "    function set_focus() {\n",
       "        canvas.focus();\n",
       "        canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    window.setTimeout(set_focus, 100);\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function () {\n",
       "    var fig = this;\n",
       "\n",
       "    var toolbar = document.createElement('div');\n",
       "    toolbar.classList = 'mpl-toolbar';\n",
       "    this.root.appendChild(toolbar);\n",
       "\n",
       "    function on_click_closure(name) {\n",
       "        return function (_event) {\n",
       "            return fig.toolbar_button_onclick(name);\n",
       "        };\n",
       "    }\n",
       "\n",
       "    function on_mouseover_closure(tooltip) {\n",
       "        return function (event) {\n",
       "            if (!event.currentTarget.disabled) {\n",
       "                return fig.toolbar_button_onmouseover(tooltip);\n",
       "            }\n",
       "        };\n",
       "    }\n",
       "\n",
       "    fig.buttons = {};\n",
       "    var buttonGroup = document.createElement('div');\n",
       "    buttonGroup.classList = 'mpl-button-group';\n",
       "    for (var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            /* Instead of a spacer, we start a new button group. */\n",
       "            if (buttonGroup.hasChildNodes()) {\n",
       "                toolbar.appendChild(buttonGroup);\n",
       "            }\n",
       "            buttonGroup = document.createElement('div');\n",
       "            buttonGroup.classList = 'mpl-button-group';\n",
       "            continue;\n",
       "        }\n",
       "\n",
       "        var button = (fig.buttons[name] = document.createElement('button'));\n",
       "        button.classList = 'mpl-widget';\n",
       "        button.setAttribute('role', 'button');\n",
       "        button.setAttribute('aria-disabled', 'false');\n",
       "        button.addEventListener('click', on_click_closure(method_name));\n",
       "        button.addEventListener('mouseover', on_mouseover_closure(tooltip));\n",
       "\n",
       "        var icon_img = document.createElement('img');\n",
       "        icon_img.src = '_images/' + image + '.png';\n",
       "        icon_img.srcset = '_images/' + image + '_large.png 2x';\n",
       "        icon_img.alt = tooltip;\n",
       "        button.appendChild(icon_img);\n",
       "\n",
       "        buttonGroup.appendChild(button);\n",
       "    }\n",
       "\n",
       "    if (buttonGroup.hasChildNodes()) {\n",
       "        toolbar.appendChild(buttonGroup);\n",
       "    }\n",
       "\n",
       "    var fmt_picker = document.createElement('select');\n",
       "    fmt_picker.classList = 'mpl-widget';\n",
       "    toolbar.appendChild(fmt_picker);\n",
       "    this.format_dropdown = fmt_picker;\n",
       "\n",
       "    for (var ind in mpl.extensions) {\n",
       "        var fmt = mpl.extensions[ind];\n",
       "        var option = document.createElement('option');\n",
       "        option.selected = fmt === mpl.default_extension;\n",
       "        option.innerHTML = fmt;\n",
       "        fmt_picker.appendChild(option);\n",
       "    }\n",
       "\n",
       "    var status_bar = document.createElement('span');\n",
       "    status_bar.classList = 'mpl-message';\n",
       "    toolbar.appendChild(status_bar);\n",
       "    this.message = status_bar;\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.request_resize = function (x_pixels, y_pixels) {\n",
       "    // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
       "    // which will in turn request a refresh of the image.\n",
       "    this.send_message('resize', { width: x_pixels, height: y_pixels });\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.send_message = function (type, properties) {\n",
       "    properties['type'] = type;\n",
       "    properties['figure_id'] = this.id;\n",
       "    this.ws.send(JSON.stringify(properties));\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.send_draw_message = function () {\n",
       "    if (!this.waiting) {\n",
       "        this.waiting = true;\n",
       "        this.ws.send(JSON.stringify({ type: 'draw', figure_id: this.id }));\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_save = function (fig, _msg) {\n",
       "    var format_dropdown = fig.format_dropdown;\n",
       "    var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
       "    fig.ondownload(fig, format);\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_resize = function (fig, msg) {\n",
       "    var size = msg['size'];\n",
       "    if (size[0] !== fig.canvas.width || size[1] !== fig.canvas.height) {\n",
       "        fig._resize_canvas(size[0], size[1], msg['forward']);\n",
       "        fig.send_message('refresh', {});\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_rubberband = function (fig, msg) {\n",
       "    var x0 = msg['x0'] / fig.ratio;\n",
       "    var y0 = (fig.canvas.height - msg['y0']) / fig.ratio;\n",
       "    var x1 = msg['x1'] / fig.ratio;\n",
       "    var y1 = (fig.canvas.height - msg['y1']) / fig.ratio;\n",
       "    x0 = Math.floor(x0) + 0.5;\n",
       "    y0 = Math.floor(y0) + 0.5;\n",
       "    x1 = Math.floor(x1) + 0.5;\n",
       "    y1 = Math.floor(y1) + 0.5;\n",
       "    var min_x = Math.min(x0, x1);\n",
       "    var min_y = Math.min(y0, y1);\n",
       "    var width = Math.abs(x1 - x0);\n",
       "    var height = Math.abs(y1 - y0);\n",
       "\n",
       "    fig.rubberband_context.clearRect(\n",
       "        0,\n",
       "        0,\n",
       "        fig.canvas.width / fig.ratio,\n",
       "        fig.canvas.height / fig.ratio\n",
       "    );\n",
       "\n",
       "    fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_figure_label = function (fig, msg) {\n",
       "    // Updates the figure title.\n",
       "    fig.header.textContent = msg['label'];\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_cursor = function (fig, msg) {\n",
       "    var cursor = msg['cursor'];\n",
       "    switch (cursor) {\n",
       "        case 0:\n",
       "            cursor = 'pointer';\n",
       "            break;\n",
       "        case 1:\n",
       "            cursor = 'default';\n",
       "            break;\n",
       "        case 2:\n",
       "            cursor = 'crosshair';\n",
       "            break;\n",
       "        case 3:\n",
       "            cursor = 'move';\n",
       "            break;\n",
       "    }\n",
       "    fig.rubberband_canvas.style.cursor = cursor;\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_message = function (fig, msg) {\n",
       "    fig.message.textContent = msg['message'];\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_draw = function (fig, _msg) {\n",
       "    // Request the server to send over a new figure.\n",
       "    fig.send_draw_message();\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_image_mode = function (fig, msg) {\n",
       "    fig.image_mode = msg['mode'];\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_history_buttons = function (fig, msg) {\n",
       "    for (var key in msg) {\n",
       "        if (!(key in fig.buttons)) {\n",
       "            continue;\n",
       "        }\n",
       "        fig.buttons[key].disabled = !msg[key];\n",
       "        fig.buttons[key].setAttribute('aria-disabled', !msg[key]);\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_navigate_mode = function (fig, msg) {\n",
       "    if (msg['mode'] === 'PAN') {\n",
       "        fig.buttons['Pan'].classList.add('active');\n",
       "        fig.buttons['Zoom'].classList.remove('active');\n",
       "    } else if (msg['mode'] === 'ZOOM') {\n",
       "        fig.buttons['Pan'].classList.remove('active');\n",
       "        fig.buttons['Zoom'].classList.add('active');\n",
       "    } else {\n",
       "        fig.buttons['Pan'].classList.remove('active');\n",
       "        fig.buttons['Zoom'].classList.remove('active');\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function () {\n",
       "    // Called whenever the canvas gets updated.\n",
       "    this.send_message('ack', {});\n",
       "};\n",
       "\n",
       "// A function to construct a web socket function for onmessage handling.\n",
       "// Called in the figure constructor.\n",
       "mpl.figure.prototype._make_on_message_function = function (fig) {\n",
       "    return function socket_on_message(evt) {\n",
       "        if (evt.data instanceof Blob) {\n",
       "            var img = evt.data;\n",
       "            if (img.type !== 'image/png') {\n",
       "                /* FIXME: We get \"Resource interpreted as Image but\n",
       "                 * transferred with MIME type text/plain:\" errors on\n",
       "                 * Chrome.  But how to set the MIME type?  It doesn't seem\n",
       "                 * to be part of the websocket stream */\n",
       "                img.type = 'image/png';\n",
       "            }\n",
       "\n",
       "            /* Free the memory for the previous frames */\n",
       "            if (fig.imageObj.src) {\n",
       "                (window.URL || window.webkitURL).revokeObjectURL(\n",
       "                    fig.imageObj.src\n",
       "                );\n",
       "            }\n",
       "\n",
       "            fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
       "                img\n",
       "            );\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        } else if (\n",
       "            typeof evt.data === 'string' &&\n",
       "            evt.data.slice(0, 21) === 'data:image/png;base64'\n",
       "        ) {\n",
       "            fig.imageObj.src = evt.data;\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        var msg = JSON.parse(evt.data);\n",
       "        var msg_type = msg['type'];\n",
       "\n",
       "        // Call the  \"handle_{type}\" callback, which takes\n",
       "        // the figure and JSON message as its only arguments.\n",
       "        try {\n",
       "            var callback = fig['handle_' + msg_type];\n",
       "        } catch (e) {\n",
       "            console.log(\n",
       "                \"No handler for the '\" + msg_type + \"' message type: \",\n",
       "                msg\n",
       "            );\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        if (callback) {\n",
       "            try {\n",
       "                // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
       "                callback(fig, msg);\n",
       "            } catch (e) {\n",
       "                console.log(\n",
       "                    \"Exception inside the 'handler_\" + msg_type + \"' callback:\",\n",
       "                    e,\n",
       "                    e.stack,\n",
       "                    msg\n",
       "                );\n",
       "            }\n",
       "        }\n",
       "    };\n",
       "};\n",
       "\n",
       "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
       "mpl.findpos = function (e) {\n",
       "    //this section is from http://www.quirksmode.org/js/events_properties.html\n",
       "    var targ;\n",
       "    if (!e) {\n",
       "        e = window.event;\n",
       "    }\n",
       "    if (e.target) {\n",
       "        targ = e.target;\n",
       "    } else if (e.srcElement) {\n",
       "        targ = e.srcElement;\n",
       "    }\n",
       "    if (targ.nodeType === 3) {\n",
       "        // defeat Safari bug\n",
       "        targ = targ.parentNode;\n",
       "    }\n",
       "\n",
       "    // pageX,Y are the mouse positions relative to the document\n",
       "    var boundingRect = targ.getBoundingClientRect();\n",
       "    var x = e.pageX - (boundingRect.left + document.body.scrollLeft);\n",
       "    var y = e.pageY - (boundingRect.top + document.body.scrollTop);\n",
       "\n",
       "    return { x: x, y: y };\n",
       "};\n",
       "\n",
       "/*\n",
       " * return a copy of an object with only non-object keys\n",
       " * we need this to avoid circular references\n",
       " * http://stackoverflow.com/a/24161582/3208463\n",
       " */\n",
       "function simpleKeys(original) {\n",
       "    return Object.keys(original).reduce(function (obj, key) {\n",
       "        if (typeof original[key] !== 'object') {\n",
       "            obj[key] = original[key];\n",
       "        }\n",
       "        return obj;\n",
       "    }, {});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.mouse_event = function (event, name) {\n",
       "    var canvas_pos = mpl.findpos(event);\n",
       "\n",
       "    if (name === 'button_press') {\n",
       "        this.canvas.focus();\n",
       "        this.canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    var x = canvas_pos.x * this.ratio;\n",
       "    var y = canvas_pos.y * this.ratio;\n",
       "\n",
       "    this.send_message(name, {\n",
       "        x: x,\n",
       "        y: y,\n",
       "        button: event.button,\n",
       "        step: event.step,\n",
       "        guiEvent: simpleKeys(event),\n",
       "    });\n",
       "\n",
       "    /* This prevents the web browser from automatically changing to\n",
       "     * the text insertion cursor when the button is pressed.  We want\n",
       "     * to control all of the cursor setting manually through the\n",
       "     * 'cursor' event from matplotlib */\n",
       "    event.preventDefault();\n",
       "    return false;\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function (_event, _name) {\n",
       "    // Handle any extra behaviour associated with a key event\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.key_event = function (event, name) {\n",
       "    // Prevent repeat events\n",
       "    if (name === 'key_press') {\n",
       "        if (event.key === this._key) {\n",
       "            return;\n",
       "        } else {\n",
       "            this._key = event.key;\n",
       "        }\n",
       "    }\n",
       "    if (name === 'key_release') {\n",
       "        this._key = null;\n",
       "    }\n",
       "\n",
       "    var value = '';\n",
       "    if (event.ctrlKey && event.key !== 'Control') {\n",
       "        value += 'ctrl+';\n",
       "    }\n",
       "    else if (event.altKey && event.key !== 'Alt') {\n",
       "        value += 'alt+';\n",
       "    }\n",
       "    else if (event.shiftKey && event.key !== 'Shift') {\n",
       "        value += 'shift+';\n",
       "    }\n",
       "\n",
       "    value += 'k' + event.key;\n",
       "\n",
       "    this._key_event_extra(event, name);\n",
       "\n",
       "    this.send_message(name, { key: value, guiEvent: simpleKeys(event) });\n",
       "    return false;\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onclick = function (name) {\n",
       "    if (name === 'download') {\n",
       "        this.handle_save(this, null);\n",
       "    } else {\n",
       "        this.send_message('toolbar_button', { name: name });\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onmouseover = function (tooltip) {\n",
       "    this.message.textContent = tooltip;\n",
       "};\n",
       "\n",
       "///////////////// REMAINING CONTENT GENERATED BY embed_js.py /////////////////\n",
       "// prettier-ignore\n",
       "var _JSXTOOLS_RESIZE_OBSERVER=function(A){var t,i=new WeakMap,n=new WeakMap,a=new WeakMap,r=new WeakMap,o=new Set;function s(e){if(!(this instanceof s))throw new TypeError(\"Constructor requires 'new' operator\");i.set(this,e)}function h(){throw new TypeError(\"Function is not a constructor\")}function c(e,t,i,n){e=0 in arguments?Number(arguments[0]):0,t=1 in arguments?Number(arguments[1]):0,i=2 in arguments?Number(arguments[2]):0,n=3 in arguments?Number(arguments[3]):0,this.right=(this.x=this.left=e)+(this.width=i),this.bottom=(this.y=this.top=t)+(this.height=n),Object.freeze(this)}function d(){t=requestAnimationFrame(d);var s=new WeakMap,p=new Set;o.forEach((function(t){r.get(t).forEach((function(i){var r=t instanceof window.SVGElement,o=a.get(t),d=r?0:parseFloat(o.paddingTop),f=r?0:parseFloat(o.paddingRight),l=r?0:parseFloat(o.paddingBottom),u=r?0:parseFloat(o.paddingLeft),g=r?0:parseFloat(o.borderTopWidth),m=r?0:parseFloat(o.borderRightWidth),w=r?0:parseFloat(o.borderBottomWidth),b=u+f,F=d+l,v=(r?0:parseFloat(o.borderLeftWidth))+m,W=g+w,y=r?0:t.offsetHeight-W-t.clientHeight,E=r?0:t.offsetWidth-v-t.clientWidth,R=b+v,z=F+W,M=r?t.width:parseFloat(o.width)-R-E,O=r?t.height:parseFloat(o.height)-z-y;if(n.has(t)){var k=n.get(t);if(k[0]===M&&k[1]===O)return}n.set(t,[M,O]);var S=Object.create(h.prototype);S.target=t,S.contentRect=new c(u,d,M,O),s.has(i)||(s.set(i,[]),p.add(i)),s.get(i).push(S)}))})),p.forEach((function(e){i.get(e).call(e,s.get(e),e)}))}return s.prototype.observe=function(i){if(i instanceof window.Element){r.has(i)||(r.set(i,new Set),o.add(i),a.set(i,window.getComputedStyle(i)));var n=r.get(i);n.has(this)||n.add(this),cancelAnimationFrame(t),t=requestAnimationFrame(d)}},s.prototype.unobserve=function(i){if(i instanceof window.Element&&r.has(i)){var n=r.get(i);n.has(this)&&(n.delete(this),n.size||(r.delete(i),o.delete(i))),n.size||r.delete(i),o.size||cancelAnimationFrame(t)}},A.DOMRectReadOnly=c,A.ResizeObserver=s,A.ResizeObserverEntry=h,A}; // eslint-disable-line\n",
       "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Left button pans, Right button zooms\\nx/y fixes axis, CTRL fixes aspect\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\\nx/y fixes axis, CTRL fixes aspect\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
       "\n",
       "mpl.extensions = [\"eps\", \"jpeg\", \"pgf\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
       "\n",
       "mpl.default_extension = \"png\";/* global mpl */\n",
       "\n",
       "var comm_websocket_adapter = function (comm) {\n",
       "    // Create a \"websocket\"-like object which calls the given IPython comm\n",
       "    // object with the appropriate methods. Currently this is a non binary\n",
       "    // socket, so there is still some room for performance tuning.\n",
       "    var ws = {};\n",
       "\n",
       "    ws.binaryType = comm.kernel.ws.binaryType;\n",
       "    ws.readyState = comm.kernel.ws.readyState;\n",
       "    function updateReadyState(_event) {\n",
       "        if (comm.kernel.ws) {\n",
       "            ws.readyState = comm.kernel.ws.readyState;\n",
       "        } else {\n",
       "            ws.readyState = 3; // Closed state.\n",
       "        }\n",
       "    }\n",
       "    comm.kernel.ws.addEventListener('open', updateReadyState);\n",
       "    comm.kernel.ws.addEventListener('close', updateReadyState);\n",
       "    comm.kernel.ws.addEventListener('error', updateReadyState);\n",
       "\n",
       "    ws.close = function () {\n",
       "        comm.close();\n",
       "    };\n",
       "    ws.send = function (m) {\n",
       "        //console.log('sending', m);\n",
       "        comm.send(m);\n",
       "    };\n",
       "    // Register the callback with on_msg.\n",
       "    comm.on_msg(function (msg) {\n",
       "        //console.log('receiving', msg['content']['data'], msg);\n",
       "        var data = msg['content']['data'];\n",
       "        if (data['blob'] !== undefined) {\n",
       "            data = {\n",
       "                data: new Blob(msg['buffers'], { type: data['blob'] }),\n",
       "            };\n",
       "        }\n",
       "        // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
       "        ws.onmessage(data);\n",
       "    });\n",
       "    return ws;\n",
       "};\n",
       "\n",
       "mpl.mpl_figure_comm = function (comm, msg) {\n",
       "    // This is the function which gets called when the mpl process\n",
       "    // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
       "\n",
       "    var id = msg.content.data.id;\n",
       "    // Get hold of the div created by the display call when the Comm\n",
       "    // socket was opened in Python.\n",
       "    var element = document.getElementById(id);\n",
       "    var ws_proxy = comm_websocket_adapter(comm);\n",
       "\n",
       "    function ondownload(figure, _format) {\n",
       "        window.open(figure.canvas.toDataURL());\n",
       "    }\n",
       "\n",
       "    var fig = new mpl.figure(id, ws_proxy, ondownload, element);\n",
       "\n",
       "    // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
       "    // web socket which is closed, not our websocket->open comm proxy.\n",
       "    ws_proxy.onopen();\n",
       "\n",
       "    fig.parent_element = element;\n",
       "    fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
       "    if (!fig.cell_info) {\n",
       "        console.error('Failed to find cell for figure', id, fig);\n",
       "        return;\n",
       "    }\n",
       "    fig.cell_info[0].output_area.element.on(\n",
       "        'cleared',\n",
       "        { fig: fig },\n",
       "        fig._remove_fig_handler\n",
       "    );\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_close = function (fig, msg) {\n",
       "    var width = fig.canvas.width / fig.ratio;\n",
       "    fig.cell_info[0].output_area.element.off(\n",
       "        'cleared',\n",
       "        fig._remove_fig_handler\n",
       "    );\n",
       "    fig.resizeObserverInstance.unobserve(fig.canvas_div);\n",
       "\n",
       "    // Update the output cell to use the data from the current canvas.\n",
       "    fig.push_to_output();\n",
       "    var dataURL = fig.canvas.toDataURL();\n",
       "    // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
       "    // the notebook keyboard shortcuts fail.\n",
       "    IPython.keyboard_manager.enable();\n",
       "    fig.parent_element.innerHTML =\n",
       "        '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "    fig.close_ws(fig, msg);\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.close_ws = function (fig, msg) {\n",
       "    fig.send_message('closing', msg);\n",
       "    // fig.ws.close()\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.push_to_output = function (_remove_interactive) {\n",
       "    // Turn the data on the canvas into data in the output cell.\n",
       "    var width = this.canvas.width / this.ratio;\n",
       "    var dataURL = this.canvas.toDataURL();\n",
       "    this.cell_info[1]['text/html'] =\n",
       "        '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function () {\n",
       "    // Tell IPython that the notebook contents must change.\n",
       "    IPython.notebook.set_dirty(true);\n",
       "    this.send_message('ack', {});\n",
       "    var fig = this;\n",
       "    // Wait a second, then push the new image to the DOM so\n",
       "    // that it is saved nicely (might be nice to debounce this).\n",
       "    setTimeout(function () {\n",
       "        fig.push_to_output();\n",
       "    }, 1000);\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function () {\n",
       "    var fig = this;\n",
       "\n",
       "    var toolbar = document.createElement('div');\n",
       "    toolbar.classList = 'btn-toolbar';\n",
       "    this.root.appendChild(toolbar);\n",
       "\n",
       "    function on_click_closure(name) {\n",
       "        return function (_event) {\n",
       "            return fig.toolbar_button_onclick(name);\n",
       "        };\n",
       "    }\n",
       "\n",
       "    function on_mouseover_closure(tooltip) {\n",
       "        return function (event) {\n",
       "            if (!event.currentTarget.disabled) {\n",
       "                return fig.toolbar_button_onmouseover(tooltip);\n",
       "            }\n",
       "        };\n",
       "    }\n",
       "\n",
       "    fig.buttons = {};\n",
       "    var buttonGroup = document.createElement('div');\n",
       "    buttonGroup.classList = 'btn-group';\n",
       "    var button;\n",
       "    for (var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            /* Instead of a spacer, we start a new button group. */\n",
       "            if (buttonGroup.hasChildNodes()) {\n",
       "                toolbar.appendChild(buttonGroup);\n",
       "            }\n",
       "            buttonGroup = document.createElement('div');\n",
       "            buttonGroup.classList = 'btn-group';\n",
       "            continue;\n",
       "        }\n",
       "\n",
       "        button = fig.buttons[name] = document.createElement('button');\n",
       "        button.classList = 'btn btn-default';\n",
       "        button.href = '#';\n",
       "        button.title = name;\n",
       "        button.innerHTML = '<i class=\"fa ' + image + ' fa-lg\"></i>';\n",
       "        button.addEventListener('click', on_click_closure(method_name));\n",
       "        button.addEventListener('mouseover', on_mouseover_closure(tooltip));\n",
       "        buttonGroup.appendChild(button);\n",
       "    }\n",
       "\n",
       "    if (buttonGroup.hasChildNodes()) {\n",
       "        toolbar.appendChild(buttonGroup);\n",
       "    }\n",
       "\n",
       "    // Add the status bar.\n",
       "    var status_bar = document.createElement('span');\n",
       "    status_bar.classList = 'mpl-message pull-right';\n",
       "    toolbar.appendChild(status_bar);\n",
       "    this.message = status_bar;\n",
       "\n",
       "    // Add the close button to the window.\n",
       "    var buttongrp = document.createElement('div');\n",
       "    buttongrp.classList = 'btn-group inline pull-right';\n",
       "    button = document.createElement('button');\n",
       "    button.classList = 'btn btn-mini btn-primary';\n",
       "    button.href = '#';\n",
       "    button.title = 'Stop Interaction';\n",
       "    button.innerHTML = '<i class=\"fa fa-power-off icon-remove icon-large\"></i>';\n",
       "    button.addEventListener('click', function (_evt) {\n",
       "        fig.handle_close(fig, {});\n",
       "    });\n",
       "    button.addEventListener(\n",
       "        'mouseover',\n",
       "        on_mouseover_closure('Stop Interaction')\n",
       "    );\n",
       "    buttongrp.appendChild(button);\n",
       "    var titlebar = this.root.querySelector('.ui-dialog-titlebar');\n",
       "    titlebar.insertBefore(buttongrp, titlebar.firstChild);\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._remove_fig_handler = function (event) {\n",
       "    var fig = event.data.fig;\n",
       "    if (event.target !== this) {\n",
       "        // Ignore bubbled events from children.\n",
       "        return;\n",
       "    }\n",
       "    fig.close_ws(fig, {});\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function (el) {\n",
       "    el.style.boxSizing = 'content-box'; // override notebook setting of border-box.\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function (el) {\n",
       "    // this is important to make the div 'focusable\n",
       "    el.setAttribute('tabindex', 0);\n",
       "    // reach out to IPython and tell the keyboard manager to turn it's self\n",
       "    // off when our div gets focus\n",
       "\n",
       "    // location in version 3\n",
       "    if (IPython.notebook.keyboard_manager) {\n",
       "        IPython.notebook.keyboard_manager.register_events(el);\n",
       "    } else {\n",
       "        // location in version 2\n",
       "        IPython.keyboard_manager.register_events(el);\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function (event, _name) {\n",
       "    var manager = IPython.notebook.keyboard_manager;\n",
       "    if (!manager) {\n",
       "        manager = IPython.keyboard_manager;\n",
       "    }\n",
       "\n",
       "    // Check for shift+enter\n",
       "    if (event.shiftKey && event.which === 13) {\n",
       "        this.canvas_div.blur();\n",
       "        // select the cell after this one\n",
       "        var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n",
       "        IPython.notebook.select(index + 1);\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_save = function (fig, _msg) {\n",
       "    fig.ondownload(fig, null);\n",
       "};\n",
       "\n",
       "mpl.find_output_cell = function (html_output) {\n",
       "    // Return the cell and output element which can be found *uniquely* in the notebook.\n",
       "    // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
       "    // IPython event is triggered only after the cells have been serialised, which for\n",
       "    // our purposes (turning an active figure into a static one), is too late.\n",
       "    var cells = IPython.notebook.get_cells();\n",
       "    var ncells = cells.length;\n",
       "    for (var i = 0; i < ncells; i++) {\n",
       "        var cell = cells[i];\n",
       "        if (cell.cell_type === 'code') {\n",
       "            for (var j = 0; j < cell.output_area.outputs.length; j++) {\n",
       "                var data = cell.output_area.outputs[j];\n",
       "                if (data.data) {\n",
       "                    // IPython >= 3 moved mimebundle to data attribute of output\n",
       "                    data = data.data;\n",
       "                }\n",
       "                if (data['text/html'] === html_output) {\n",
       "                    return [cell, data, j];\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "};\n",
       "\n",
       "// Register the function which deals with the matplotlib target/channel.\n",
       "// The kernel may be null if the page has been refreshed.\n",
       "if (IPython.notebook.kernel !== null) {\n",
       "    IPython.notebook.kernel.comm_manager.register_target(\n",
       "        'matplotlib',\n",
       "        mpl.mpl_figure_comm\n",
       "    );\n",
       "}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"360\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 绘制b的变化和mse变化的关系\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib notebook\n",
    "\n",
    "fig = plt.figure(figsize=(6, 6), dpi=60)\n",
    "\n",
    "plt.plot(b_history, mse_history)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee0ce28b",
   "metadata": {},
   "source": [
    "## 使用代码对m和b求偏导"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "87830348",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import symbols, Sum, diff\n",
    "\n",
    "\n",
    "i, x_i, m, b, Actual_i = symbols(\"i, x_i, m, b, Actual_i\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e798b49a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sum_{i=1}^{6} \\left(- Actual_{i} + b + m x_{i}\\right)^{2}$"
      ],
      "text/plain": [
       "Sum((-Actual_i + b + m*x_i)**2, (i, 1, 6))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mse = Sum((m * x_i + b - Actual_i) ** 2, (i, 1, 6))\n",
    "mse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bcb2b1c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sum_{i=1}^{6} 2 x_{i} \\left(- Actual_{i} + b + m x_{i}\\right)$"
      ],
      "text/plain": [
       "Sum(2*x_i*(-Actual_i + b + m*x_i), (i, 1, 6))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(mse, m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ecb35913",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sum_{i=1}^{6} \\left(- 2 Actual_{i} + 2 b + 2 m x_{i}\\right)$"
      ],
      "text/plain": [
       "Sum(-2*Actual_i + 2*b + 2*m*x_i, (i, 1, 6))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(mse, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21fed2c5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
